﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DoeObjects;
using DoeLibsMVC.Security;

namespace DoeLibsMVC.Controllers.api
{
    public class NotificationController : BaseApiController
    {
        /// <summary>
        /// returns all notifications for the logged in user
        /// </summary>
        /// <returns></returns>
        [Security.BasicAuthorize]
        public IEnumerable<Notification> Get()
        {
            return Notification.getAllForUser(User.UserId);
        }

        /// <summary>
        /// returns a specific notification
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Security.BasicAuthorize]
        public HttpResponseMessage Get(int id)
        {
            Notification notification = Notification.getById(id);

            if (notification != null)
            {
                if (notification.Recipient.UserId != User.UserId)
                {
                    return Request.CreateResponse(HttpStatusCode.Forbidden);
                }
                else
                {
                    return Request.CreateResponse<Notification>(HttpStatusCode.OK, notification);
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
        }

        /// <summary>
        /// returns all read/unread notifications for the user
        /// </summary>
        /// <param name="read">Type: read/unread</param>
        /// <returns></returns>
        [Security.BasicAuthorize]
        public HttpResponseMessage Get(string read)
        {
            List<Notification> result = Notification.getAllForUser(User.UserId, (read != "true"));

            return Request.CreateResponse<IEnumerable<Notification>>(HttpStatusCode.OK, result);
        }

        /// <summary>
        /// marks a notification as read
        /// </summary>
        /// <param name="id"></param>
        /// <param name="read"></param>
        /// <returns></returns>
        [Security.BasicAuthorize]
        public HttpResponseMessage Put(int id, bool read)
        {
            Notification notification = Notification.getById(id);

            if (notification != null)
            {
                if (notification.Recipient.UserId != User.UserId)
                {
                    return Request.CreateResponse(HttpStatusCode.Forbidden);
                }
                else
                {
                    notification.Read = read;

                    if (notification.save())
                    {
                        return Request.CreateResponse(HttpStatusCode.OK);
                    }
                    else
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Could not update notification");
                    }
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
        }

        /// <summary>
        /// deletes a notification
        /// </summary>
        /// <param name="id">notification id</param>
        /// <returns></returns>
        [Security.BasicAuthorize]
        public HttpResponseMessage Delete(int id)
        {
            Notification notification = Notification.getById(id);

            if (notification != null)
            {
                if (notification.Recipient.UserId != User.UserId)
                {
                    return Request.CreateResponse(HttpStatusCode.Forbidden);
                }
                else
                {
                    if (Notification.delete(id))
                    {
                        return Request.CreateResponse(HttpStatusCode.OK);
                    }
                    else
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Could not delete notification");
                    }
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
        }
    }
}
